﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Utils.Extention;

#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/9/14 21:12:57
* 文件名：@default.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace ChinaLeg.Web
{
    public partial class _default : System.Web.UI.Page
    {
        protected string title, description, keyWrods,bgImage,adScroll;
        
        protected int catid;
        protected void Page_Load(object sender, EventArgs e)
        {
            catid = 1;
            if (!this.IsPostBack)
            {
                ChinaLeg.Cache.Category.Instance.GetModel(catid, out title, out  keyWrods, out  description, out  bgImage);
                Top1.CatId = catid;
                Top1.BgImage = bgImage;
                adScroll = ChinaLeg.Cache.ScrollFocusPicture.Instance.GetList(catid);
                Footer1.divFriendLink.Visible = true;
                BindList();
                BindNews(3,oxtLeg1,oxtLeg2,9,4);
                BindNews(7, sxt1, sxt2, 9, 4);
                BindNews(4, xxt1, xxt2, 8, 4);
                BindNews(6, zgt1, zgt2, 8, 4);
                BindNews(5,bdcxt1,bdcxt2,8,4);
                BindNews(8,dtsx1,dtxs2,8,4);
                BindNews(9,tbzhsx1,tbzhsx2,7,4);
                BindNews(10,tbxf1,tbxf2,7,4);
               
                BindCase();
            }
           
        }

        protected string GetSubString(object obj, int len)
        {
            return obj.ToTrim().SubStringByBytes(len,false);
        }

        private void BindCase()
        {
            //rptCase.DataSource = ChinaLeg.BLL.CaseShow.Instance.GetList(" and catid=59 ", 2, "ShowOrder", 0);
            //rptCase.DataBind();

            rptStory.DataSource = ChinaLeg.BLL.CaseShow.Instance.GetList(" and catid=48 ", 4, "ShowOrder", 0); 
            rptStory.DataBind();
        }

        private void BindList()
        {
            List<ChinaLeg.Model.News> list=ChinaLeg.BLL.News.Instance.GetList(" and catid=17 and IndexCommend=1", 4, "ShowOrder", 0);

            List<ChinaLeg.Model.News> temp = ChinaLeg.BLL.News.Instance.GetList(" and catid=17", 8, "Id", 1);
            foreach (ChinaLeg.Model.News model in temp)
            {
                list.Add(model);
            }

            rpt17.DataSource = list;
            rpt17.DataBind();

            list = ChinaLeg.BLL.News.Instance.GetList(" and catid=16 and IndexCommend=1", 2, "ShowOrder", 0);
            temp = ChinaLeg.BLL.News.Instance.GetList(" and catid=16", 4, "Id", 1);
            foreach (ChinaLeg.Model.News model in temp)
            {
                list.Add(model);
            }
            rpt16.DataSource = list;
            rpt16.DataBind();
            

            list = ChinaLeg.BLL.News.Instance.GetList(" and catid=15 and IndexCommend=1", 2, "ShowOrder", 0);
            temp = ChinaLeg.BLL.News.Instance.GetList(" and catid=15", 4, "Id", 1);
            foreach (ChinaLeg.Model.News model in temp)
            {
                list.Add(model);
            }
            rpt15.DataSource = list;
            rpt15.DataBind();
        }

        private void BindNews(int catid,System.Web.UI.HtmlControls.HtmlContainerControl ul1,System.Web.UI.HtmlControls.HtmlContainerControl ul2,int allSize,int top)
        {
            List<ChinaLeg.Model.News> list = ChinaLeg.BLL.News.Instance.GetList(" and IndexCommend=1 and catid in ("+ ChinaLeg.Cache.Category.Instance.GetChildLevelIds(catid) +") ", 2, "ShowOrder", 0);

            List<ChinaLeg.Model.News> temp = ChinaLeg.BLL.News.Instance.GetList(" and catid in (" + ChinaLeg.Cache.Category.Instance.GetChildLevelIds(catid) + ") ", (top-2+1), "Id", 1);
            foreach (ChinaLeg.Model.News model in temp)
            {
                list.Add(model);
            }
            string str = "";

            var items = list.Take(top);
            foreach (var item in items)
            {
                str += string.Format("<li><a target=\"_blank\" href='{0}' title='{1}'>{1}</a></li>", ChinaLeg.Web.Lib.WebLinks.GetNewsLinks(item.Id),item.Title);
            }

            items = list.Skip(top).Take(1);

            foreach (var item in items)
            {
                str += string.Format("<li><a target=\"_blank\" href='{0}' title='{1}'>{1}</a><a target='_blank' href='{2}'>......|详细|</a></li>", ChinaLeg.Web.Lib.WebLinks.GetNewsLinks(item.Id), item.Title.SubStringByBytes(18,false) , ChinaLeg.Web.Lib.WebLinks.GetCatLinks(catid));
            }
            ul1.InnerHtml = str;

            str = "";
            List<ChinaLeg.Model.Category> nodes = ChinaLeg.Cache.Category.Instance.GetChildLevelList(catid);
            foreach (var item in nodes)
            {
                str += string.Format("<li><a target=\"_blank\" href='{0}' title='{1}'>{1}</a></li>", GetNewsLink(item.PageType,item.Id), item.CatName);
            }
            ul2.InnerHtml = str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageType"></param>
        /// <returns></returns>
        protected string GetNewsLink(int PageType,int childCatId)
        {
            string url = "";
            switch (PageType)
            {
                case 2:
                    url = ChinaLeg.Web.Lib.WebLinks.GetListLinks(childCatId);
                    break;
                case 3:
                    url = ChinaLeg.Web.Lib.WebLinks.GetCaseCatLinks(childCatId);
                    break;
                case 4:
                    url = ChinaLeg.Web.Lib.WebLinks.GetCasePictrue(childCatId);
                    break;
            }
            return url;
        }

       
    }
}